package com.server.web.controller.pinggu;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.server.common.annotation.Log;
import com.server.common.core.controller.BaseController;
import com.server.common.core.domain.AjaxResult;
import com.server.common.core.page.TableDataInfo;
import com.server.common.enums.BusinessType;
import com.server.common.utils.poi.ExcelUtil;
import com.server.pinggu.domain.SenTags;
import com.server.pinggu.service.ISenTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ltao
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/sen/tags")
public class SenTagsController extends BaseController
{
    @Autowired
    private ISenTagsService senTagsService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SenTags senTags)
    {
        startPage();
        List<SenTags> list = senTagsService.queryList(senTags);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SenTags senTags)
    {
        List<SenTags> list = senTagsService.queryList(senTags);
        ExcelUtil<SenTags> util = new ExcelUtil<SenTags>(SenTags.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(senTagsService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SenTags senTags)
    {
        if (senTags.getFid()==null){
            throw new  RuntimeException("fid 不能为空");
        }
        SenTags value = senTagsService.getBaseMapper().selectOne(new LambdaQueryWrapper<SenTags>().eq(SenTags::getFid, senTags.getFid()));
        if (value!=null){
            LambdaUpdateWrapper<SenTags> lambdaUpdateWrapper = new LambdaUpdateWrapper();
            lambdaUpdateWrapper.set(SenTags::getCont,senTags.getCont());
            lambdaUpdateWrapper.eq(SenTags::getId,value.getId());
            return toAjax(senTagsService.update(lambdaUpdateWrapper));
        }else{
            senTags.setDtime(System.currentTimeMillis()/1000);
            return toAjax(senTagsService.save(senTags));
        }
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SenTags senTags)
    {
        return toAjax(senTagsService.updateById(senTags));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(senTagsService.removeByIds(Arrays.asList(ids)));
    }
}
